package com.fastwire.carriere.discovery;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.xml.ws.Holder;

import org.tmforum.mtop.fmw.xsd.gen.v1.AliasNameListType;
import org.tmforum.mtop.fmw.xsd.hdr.v1.CommunicationPatternType;
import org.tmforum.mtop.fmw.xsd.hdr.v1.CommunicationStyleType;
import org.tmforum.mtop.fmw.xsd.hdr.v1.Header;
import org.tmforum.mtop.fmw.xsd.nam.v1.NamingAttributeType;
import org.tmforum.mtop.fmw.xsd.nam.v1.RelativeDistinguishNameType;
import org.tmforum.mtop.mri.wsdl.conr.v1_0.ConnectionRetrievalHttp;
import org.tmforum.mtop.mri.wsdl.conr.v1_0.ConnectionRetrievalRPC;
import org.tmforum.mtop.mri.wsdl.conr.v1_0.GetAllCrossConnectionsException;
import org.tmforum.mtop.mri.wsdl.eir.v1_0.EquipmentInventoryRetrievalHttp;
import org.tmforum.mtop.mri.wsdl.eir.v1_0.EquipmentInventoryRetrievalRPC;
import org.tmforum.mtop.mri.wsdl.eir.v1_0.GetAllEquipmentException;
import org.tmforum.mtop.mri.wsdl.fdr.v1_0.FlowDomainRetrieval;
import org.tmforum.mtop.mri.wsdl.fdr.v1_0.FlowDomainRetrievalRPC;
import org.tmforum.mtop.mri.wsdl.fdr.v1_0.GetAllFlowDomainFragmentsException;
import org.tmforum.mtop.mri.wsdl.mer.v1_0.GetAllManagedElementsException;
import org.tmforum.mtop.mri.wsdl.mer.v1_0.GetManagedElementException;
import org.tmforum.mtop.mri.wsdl.mer.v1_0.ManagedElementRetrievalHttp;
import org.tmforum.mtop.mri.wsdl.mer.v1_0.ManagedElementRetrievalRPC;
import org.tmforum.mtop.mri.wsdl.tcpr.v1_0.GetAllTrafficConditioningProfileNamesException;
import org.tmforum.mtop.mri.wsdl.tcpr.v1_0.TrafficConditioningProfileRetrievalHttp;
import org.tmforum.mtop.mri.wsdl.tcpr.v1_0.TrafficConditioningProfileRetrievalRPC;
import org.tmforum.mtop.mri.wsdl.tlr.v1_0.GetAllTopologicalLinksException;
import org.tmforum.mtop.mri.wsdl.tlr.v1_0.TopologicalLinkRetrievalHttp;
import org.tmforum.mtop.mri.wsdl.tlr.v1_0.TopologicalLinkRetrievalRPC;
import org.tmforum.mtop.mri.wsdl.tpr.v1_0.GetAllPhysicalTerminationPointsWithoutFtpsException;
import org.tmforum.mtop.mri.wsdl.tpr.v1_0.TerminationPointRetrievalHttp;
import org.tmforum.mtop.mri.wsdl.tpr.v1_0.TerminationPointRetrievalRPC;
import org.tmforum.mtop.mri.xsd.conr.v1.GetCrossConnectionsRequestType;
import org.tmforum.mtop.mri.xsd.conr.v1.MultipleCrossConnectionObjectsResponseType;
import org.tmforum.mtop.mri.xsd.eir.v1.GetAllEquipmentRequestType;
import org.tmforum.mtop.mri.xsd.fdr.v1.GetAllFlowDomainFragmentsRequest;
import org.tmforum.mtop.mri.xsd.fdr.v1.GetAllFlowDomainFragmentsResponse;
import org.tmforum.mtop.mri.xsd.mer.v1.GetAllManagedElementsRequest;
import org.tmforum.mtop.mri.xsd.mer.v1.GetManagedElementRequest;
import org.tmforum.mtop.mri.xsd.mer.v1.MultipleMeObjectsResponseType;
import org.tmforum.mtop.mri.xsd.mer.v1.SingleMeObjectResponseType;
import org.tmforum.mtop.mri.xsd.tcpr.v1.GetAllTrafficConditioningProfileNamesRequest;
import org.tmforum.mtop.mri.xsd.tcpr.v1.GetAllTrafficConditioningProfileNamesResponse;
import org.tmforum.mtop.mri.xsd.tlr.v1.GetAllTopologicalLinksRequest;
import org.tmforum.mtop.mri.xsd.tlr.v1.MultipleTlObjectsResponseType;
import org.tmforum.mtop.mri.xsd.tpr.v1.GetAllTerminationPointsType;
import org.tmforum.mtop.mri.xsd.tpr.v1.MultipleTerminationPointObjectsResponseType;
import org.tmforum.mtop.nra.xsd.pg.v1.ProtectionGroupType;
import org.tmforum.mtop.nrb.xsd.lay.v1.LayerRateListType;
import org.tmforum.mtop.nrb.xsd.lay.v1.LayerRateType;
import org.tmforum.mtop.nrf.xsd.cc.v1.CrossConnectType;
import org.tmforum.mtop.nrf.xsd.eq.v1.EquipmentType;
import org.tmforum.mtop.nrf.xsd.eq_inv.v1.EquipmentOrEquipmentHolderListType;
import org.tmforum.mtop.nrf.xsd.eq_inv.v1.EquipmentOrEquipmentHolderType;
import org.tmforum.mtop.nrf.xsd.fdfr.v1.FlowDomainFragmentType;
import org.tmforum.mtop.nrf.xsd.me.v1.ManagedElementType;
import org.tmforum.mtop.nrf.xsd.ptp.v1.PhysicalTerminationPointType;
import org.tmforum.mtop.nrf.xsd.tl.v1.TopologicalLinkType;
import org.tmforum.mtop.nrf.xsd.tp.v1.TerminationPointType;
import org.tmforum.mtop.rtm.wsdl.pr.v1_0.GetAllProtectionGroupsException;
import org.tmforum.mtop.rtm.xsd.pr.v1.GetAllProtectionGroupsRequest;
import org.tmforum.mtop.rtm.xsd.pr.v1.GetAllProtectionGroupsResponse;

import com.fastwire.carriere.common.constant.AppConstants;
import com.fastwire.carriere.common.exception.U2000ProcessException;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.controller.NetworkEntiyController;
import com.fastwire.carriere.huawei.mtopsi.ra.HuaweiMTOSIConnection;
import com.fastwire.carriere.inventory.entity.CrossConnect;
import com.fastwire.carriere.inventory.entity.Equipment;
import com.fastwire.carriere.inventory.entity.FDFr;
import com.fastwire.carriere.inventory.entity.NetworkElement;
import com.fastwire.carriere.inventory.entity.ProtectionGroup;
import com.fastwire.carriere.inventory.entity.SubTerminationPoint;
import com.fastwire.carriere.inventory.entity.TerminationPoint;
import com.fastwire.carriere.inventory.entity.TopologicalLink;
import com.fastwire.carriere.inventory.entity.TrafficConditioningProfile;
import com.fastwire.carriere.inventory.entity.VPLSExtensions;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

// TODO: Auto-generated Javadoc
/**
 * The Class NetworkEntityDiscovery.
 */
public class NetworkEntityDiscovery {

	private EquipmentInventoryRetrievalRPC equipmentInventoryRetrievalRPC;

	private ConnectionRetrievalRPC connectionRetrievalRPC;
	private FlowDomainRetrievalRPC flowDomainRetrievalRPC;
	private TerminationPointRetrievalHttp terminationPointRetrievalHttp;
	private org.tmforum.mtop.rtm.wsdl.pr.v1_0.ProtectionRetrieval_Service protectionRetrieval;

	/**
	 * Discover all ne.
	 * 
	 * @return the list
	 * @throws U2000ProcessException
	 *             the u2000 process exception
	 */
	public List<NetworkElement> discoverAllNE() throws U2000ProcessException {

		GetAllManagedElementsRequest request = new GetAllManagedElementsRequest();
		Holder<Header> headerHolder = new Holder<Header>();
		Header header = new Header();

		headerHolder.value = header;
		header.setSecurity(HuaweiMTOSIConnection.getSecurity());
		header.setCommunicationPattern(CommunicationPatternType.MULTIPLE_BATCH_RESPONSE);
		header.setCommunicationStyle(CommunicationStyleType.RPC);

		long requestedBatchSize = Long.parseLong(HuaweiMTOSIConnection
				.getNeFetchSize());
		header.setRequestedBatchSize(requestedBatchSize);
		header.setBatchSequenceNumber((long) 1);

		NamingAttributeType mdType = new NamingAttributeType();
		List<RelativeDistinguishNameType> ednList = mdType.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");
		ednList.add(mdRdn);
		request.setMdOrMlsnRef(mdType);

		Holder<MultipleMeObjectsResponseType> responseHolder = new Holder<MultipleMeObjectsResponseType>();

		List<NetworkElement> neList = Lists.newArrayList();
		try {

			ManagedElementRetrievalHttp service = new ManagedElementRetrievalHttp(
					HuaweiMTOSIConnection.getWebServiceURL(AppConstants.ME_NRF));
			CommonLogger.info(this, "check ME discovered:");
			ManagedElementRetrievalRPC port = service
					.getManagedElementRetrievalRPC();

			do {

				port.getAllManagedElements(headerHolder, request,
						responseHolder);

				CommonLogger.info(this, "check ME discovered:");

				List<ManagedElementType> meList = responseHolder.value
						.getMeList().getMe();

				CommonLogger.info(this, "batch ME discovered:"
						+ responseHolder.value.getMeList().getMe().size());

				CommonLogger.info(
						this,
						"batch Header URI"
								+ headerHolder.value.getIteratorReferenceURI());

				for (ManagedElementType managedElementType : meList) {
					NetworkElement networkElement = new NetworkElement();
					networkElement.setNeType(managedElementType
							.getProductName());
					networkElement.setName(managedElementType.getName()
							.getRdn().get(1).getValue());
					CommonLogger
							.info(this, "ne is:" + networkElement.getName());
					networkElement.setEmsNativeName(managedElementType
							.getProductName());
					networkElement.setIpAddress(managedElementType
							.getIpaddress());
					networkElement.setSoftwareVersion(managedElementType
							.getSoftwareVersion());
					networkElement.setCommunicationState(managedElementType
							.getCommunicationState().name());

					networkElement.setIsInSyncState(String
							.valueOf(managedElementType.isIsInSyncState()));

					AliasNameListType aliasNameListType = managedElementType
							.getAliasNameList();
					List<AliasNameListType.Alias> aliasNameListTypes = aliasNameListType
							.getAlias();
					networkElement.setEmsNativeName(aliasNameListTypes.get(0)
							.getAliasValue());
					neList.add(networkElement);
				}

			} while (headerHolder.value.isBatchSequenceEndOfReply()
					.booleanValue() == false);

		} catch (GetAllManagedElementsException e) {
			e.printStackTrace();

			CommonLogger.error(this, e.getFaultInfo().toString());

			throw new U2000ProcessException(e.getMessage());
		} catch (MalformedURLException e) {
			e.printStackTrace();
			throw new U2000ProcessException(
					"MalformedURLException: siacoverAlNE()" + e.getMessage());
		}

		CommonLogger.info(this, "Discovered NEs:" + neList.size());

		return neList;

	}

	public void checkNE(NetworkElement ne) throws U2000ProcessException {

		GetManagedElementRequest request = new GetManagedElementRequest();
		Holder<Header> headerHolder = HuaweiMTOSIConnection
				.getSingleResponseHeader();

		NamingAttributeType mdType = new NamingAttributeType();
		List<RelativeDistinguishNameType> ednList = mdType.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");

		RelativeDistinguishNameType neRdn = new RelativeDistinguishNameType();
		neRdn.setType("ME");
		neRdn.setValue(ne.getName());

		ednList.add(mdRdn);
		ednList.add(neRdn);
		request.setMeRef(mdType);

		Holder<SingleMeObjectResponseType> responseHolder = new Holder<SingleMeObjectResponseType>();

		try {

			ManagedElementRetrievalHttp service = new ManagedElementRetrievalHttp(
					HuaweiMTOSIConnection.getWebServiceURL(AppConstants.ME_NRF));

			ManagedElementRetrievalRPC port = service
					.getManagedElementRetrievalRPC();
			port.getManagedElement(headerHolder, request, responseHolder);

		} catch (GetManagedElementException e) {
			e.printStackTrace();

			CommonLogger.error(this, e.getFaultInfo().toString());

			throw new U2000ProcessException(e.getMessage());
		} catch (MalformedURLException e) {
			e.printStackTrace();
			throw new U2000ProcessException(
					"MalformedURLException: siacoverAlNE()" + e.getMessage());
		}

	}

	/**
	 * Discover all equipmentsby ne.
	 * 
	 * @param ne
	 *            the ne
	 * @return the list
	 * @throws U2000ProcessException
	 *             the u2000 process exception
	 */
	public Set<Equipment> discoverAllEquipmentsbyNE(NetworkElement ne)
			throws U2000ProcessException {
		String neName = ne.getName();
		CommonLogger.info(this, "input ne:" + neName);
		Holder<Header> headerHolder = HuaweiMTOSIConnection
				.getSingleResponseHeader();

		GetAllEquipmentRequestType request = new GetAllEquipmentRequestType();
		NamingAttributeType neNameAttributeType = new NamingAttributeType();
		List<RelativeDistinguishNameType> rdnList = neNameAttributeType
				.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");
		rdnList.add(mdRdn);

		RelativeDistinguishNameType neRdn = new RelativeDistinguishNameType();
		neRdn.setType("ME");
		neRdn.setValue(neName);
		rdnList.add(neRdn);

		request.setMeOrEhRef(neNameAttributeType);

		Set<Equipment> equipments = Sets.newHashSet();

		Holder<EquipmentOrEquipmentHolderListType> responseHolder = new Holder<EquipmentOrEquipmentHolderListType>();
		responseHolder.value = new EquipmentOrEquipmentHolderListType();
		try {

			if (equipmentInventoryRetrievalRPC == null) {
				EquipmentInventoryRetrievalHttp equipmentInventoryRetrievalHttp = new EquipmentInventoryRetrievalHttp(
						HuaweiMTOSIConnection
								.getWebServiceURL(AppConstants.EQUIPMENT_NRF));

				equipmentInventoryRetrievalRPC = equipmentInventoryRetrievalHttp
						.getEquipmentInventoryRetrievalRPC();
			}
			equipmentInventoryRetrievalRPC.getAllEquipment(headerHolder,
					request, responseHolder);
			EquipmentOrEquipmentHolderListType equipmentOrEquipmentHolderListType = responseHolder.value;
			List<EquipmentOrEquipmentHolderType> eqList = equipmentOrEquipmentHolderListType
					.getEoh();

			for (EquipmentOrEquipmentHolderType eq : eqList) {
				Equipment equipment = new Equipment();
				List<RelativeDistinguishNameType> rdnRtnList = null;
				if (eq.getEq() != null) {
					EquipmentType equipmentType = eq.getEq();
					equipment.setEmsNativeName(equipmentType.getAliasNameList()
							.getAlias().get(0).getAliasValue());

					rdnRtnList = equipmentType.getName().getRdn();
					equipment.setName(rdnRtnList, true);
					equipment.setEquipmentType(equipment.getEmsNativeName());

					equipment.setExpectedEquipmentObjectType(equipmentType
							.getExpectedEquipmentObjectType());
					equipment.setInstalledEquipmentObjectType(equipmentType
							.getInstalledEquipmentObjectType());
					equipment.setBomCode(equipmentType.getBomCode().trim());
					equipment.setInstalledVersion(equipmentType
							.getInstalledVersion());
					equipment.setResourceFulfillmentState(equipment
							.getResourceFulfillmentState());

				}

				else {
					equipment.setEmsNativeName(eq.getEh().getAliasNameList()
							.getAlias().get(0).getAliasValue());
					rdnRtnList = eq.getEh().getName().getRdn();
					equipment.setName(rdnRtnList, false);

				}

				equipments.add(equipment);

			}

		} catch (GetAllEquipmentException e) {
			e.printStackTrace();
			throw new U2000ProcessException(e.getMessage());
		} catch (MalformedURLException e) {
			e.printStackTrace();
			throw new U2000ProcessException(e.getMessage());
		}

		CommonLogger.info(this, "Discovered NE:" + neName + ", equipments: "
				+ equipments.size());

		return equipments;

	}

	/**
	 * Discover all cross connectsby ne.
	 * 
	 * @param ne
	 *            the ne
	 * @return the list
	 * @throws MalformedURLException
	 *             the malformed url exception
	 * @throws U2000ProcessException
	 *             the u2000 process exception
	 */
	public List<CrossConnect> discoverAllCrossConnectsbyNE(NetworkElement ne)
			throws MalformedURLException, U2000ProcessException {

		if (ne.getNeType().equals("OptiX OSN 6800")
				|| ne.getNeType().equals("OptiX OSN 8800 T32")
				|| ne.getNeType().equals("Virtual L2 NE")) {
			return Lists.newArrayList();
		}

		Holder<MultipleCrossConnectionObjectsResponseType> responseHolder = new Holder<MultipleCrossConnectionObjectsResponseType>();

		Holder<Header> headerHolder = HuaweiMTOSIConnection
				.getSingleResponseHeader();

		GetCrossConnectionsRequestType request = new GetCrossConnectionsRequestType();
		NamingAttributeType neNameAttributeType = new NamingAttributeType();
		List<RelativeDistinguishNameType> rdnList = neNameAttributeType
				.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");
		rdnList.add(mdRdn);

		RelativeDistinguishNameType neRdn = new RelativeDistinguishNameType();
		neRdn.setType("ME");
		neRdn.setValue(ne.getName());
		rdnList.add(neRdn);

		request.setMeRef(neNameAttributeType);

		LayerRateListType layerRateListType = new LayerRateListType();
		List<LayerRateType> layerRateList = layerRateListType.getLayerRate();
		LayerRateType layerRateType = new LayerRateType();
		layerRateType.setValue("LR_MPLS_Path");
		layerRateList.add(layerRateType);
		request.setConnectionRateList(layerRateListType);

		if (connectionRetrievalRPC == null) {
			ConnectionRetrievalHttp connectionRetrievalHttp = new ConnectionRetrievalHttp(
					HuaweiMTOSIConnection
							.getWebServiceURL(AppConstants.TUNNEL_NRF));
			;

			connectionRetrievalRPC = connectionRetrievalHttp
					.getConnectionRetrievalRPC();
		}
		java.util.Map<String, Object> requestContext = ((javax.xml.ws.BindingProvider) connectionRetrievalRPC)
				.getRequestContext();
		requestContext.put("set-jaxb-validation-event-handler", "false");

		List<CrossConnect> crossConnectList = new ArrayList<CrossConnect>();
		try {

			connectionRetrievalRPC.getAllCrossConnections(headerHolder,
					request, responseHolder);

			if (responseHolder.value.getCcList() != null) {

				List<CrossConnectType> ccList = responseHolder.value
						.getCcList().getCc();

				CommonLogger.info(NetworkEntiyController.class, "Discover "
						+ ccList.size() + " CrossConnections");

				for (CrossConnectType ccType : ccList) {
					CrossConnect crossConnect = new CrossConnect();
					crossConnect.setName(ccType.getName().getRdn());
					crossConnect.setAlias(ccType.getAliasNameList().getAlias());
					crossConnect.setAdministrativeState(ccType
							.getAdministrativeState().toString());
					crossConnect.setDirection(ccType.getDirection().toString());
					crossConnect
							.setIsActive(String.valueOf(ccType.isIsActive()));
					crossConnect.setIsFixed(String.valueOf(ccType.isIsFixed()));
					crossConnect
							.setIsActive(String.valueOf(ccType.isIsActive()));
					crossConnect.setTransParameters(ccType
							.getTransmissionParametersList()
							.getTransmissionParameters(), true);
					crossConnect.setAendCTP(ccType.getAEndRefList());
					crossConnect.setZendCTP(ccType.getZEndRefList());
					crossConnectList.add(crossConnect);

				}
			}
			return crossConnectList;

		} catch (GetAllCrossConnectionsException e) {
			throw new U2000ProcessException("GetAllCrossConnectionsException:"
					+ e.getMessage());

		}

	}

	/**
	 * Discover all fd frby ne.
	 * 
	 * @param neName
	 *            the ne name
	 * @return the list
	 * @throws U2000ProcessException
	 *             the u2000 process exception
	 * @throws MalformedURLException
	 *             the malformed url exception
	 */
	public List<FDFr> discoverAllFDFrbyNE(String neName)
			throws U2000ProcessException, MalformedURLException {
		GetAllFlowDomainFragmentsRequest request = new GetAllFlowDomainFragmentsRequest();
		Holder<Header> headerHolder = new Holder<Header>();
		Header header = new Header();

		headerHolder.value = header;
		header.setSecurity(HuaweiMTOSIConnection.getSecurity());
		header.setCommunicationPattern(CommunicationPatternType.SIMPLE_RESPONSE);
		header.setCommunicationStyle(CommunicationStyleType.RPC);

		NamingAttributeType mdType = new NamingAttributeType();
		List<RelativeDistinguishNameType> ednList = mdType.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");

		RelativeDistinguishNameType neRdn = new RelativeDistinguishNameType();
		neRdn.setType("ME");
		neRdn.setValue(neName);
		ednList.add(neRdn);
		ednList.add(mdRdn);

		request.setFdRef(mdType);

		List<FDFr> fdFrs = new ArrayList<FDFr>();

		try {

			if (flowDomainRetrievalRPC == null) {
				FlowDomainRetrieval flowDomainRetrieval = new FlowDomainRetrieval(
						HuaweiMTOSIConnection
								.getWebServiceURL(AppConstants.FDFR_NRF));

				flowDomainRetrievalRPC = flowDomainRetrieval
						.getFlowDomainRetrieval();
			}

			Holder<GetAllFlowDomainFragmentsResponse> responseHolder = new Holder<GetAllFlowDomainFragmentsResponse>();

			flowDomainRetrievalRPC.getAllFlowDomainFragments(headerHolder,
					request, responseHolder);
			GetAllFlowDomainFragmentsResponse response = responseHolder.value;
			List<FlowDomainFragmentType> flowDomainFragmentTypes = response
					.getFdfrList().getFdfr();
			CommonLogger.info(NetworkEntiyController.class, "Discover "
					+ flowDomainFragmentTypes.size() + " FdFrs");
			for (FlowDomainFragmentType flowDomainFragmentType : flowDomainFragmentTypes) {
				FDFr fdFr = new FDFr();

				fdFr.setName(flowDomainFragmentType.getName().getRdn());
				fdFr.setEmsNativeName(flowDomainFragmentType.getAliasNameList()
						.getAlias().get(0).getAliasValue());

				if (fdFr.getName().contains("FDFR=VSI")) {
					continue;
				}
				fdFr.setFdfrType(flowDomainFragmentType.getFdfrType());

				fdFr.setAdministrativeState(flowDomainFragmentType
						.getAdministrativeState().value());
				fdFr.setFdfrState(flowDomainFragmentType.getFdfrState().value());
				fdFr.setDirection(flowDomainFragmentType.getDirection().value());
				fdFr.setIsFlexible(String.valueOf(flowDomainFragmentType
						.isIsFlexible()));
				fdFr.setAdministrativeState(flowDomainFragmentType
						.getAdministrativeState().value());
				fdFr.setAendTp(new SubTerminationPoint(flowDomainFragmentType
						.getAEndTpDataList()));
				fdFr.setZendTp(new SubTerminationPoint(flowDomainFragmentType
						.getZEndTpDataList()));
				fdFr.setTransParameters(
						flowDomainFragmentType.getTransmissionParameters(),
						true);
				if (flowDomainFragmentType.getHwVPLSExtensions() != null) {
					fdFr.setVplsExtensions(new VPLSExtensions(
							flowDomainFragmentType.getHwVPLSExtensions()));
				}

				fdFrs.add(fdFr);

			}

			return fdFrs;

		} catch (GetAllFlowDomainFragmentsException e) {
			throw new U2000ProcessException("GetAllCrossConnectionsException:"
					+ e.getMessage());
		}

	}

	/**
	 * Discover all top toplogical links.
	 * 
	 * @return the list
	 * @throws U2000ProcessException
	 *             the u2000 process exception
	 * @throws MalformedURLException
	 *             the malformed url exception
	 */
	public List<TopologicalLink> discoverAllTopToplogicalLinks()
			throws U2000ProcessException, MalformedURLException {
		GetAllTopologicalLinksRequest request = new GetAllTopologicalLinksRequest();
		Holder<Header> headerHolder = new Holder<Header>();
		Header header = new Header();

		headerHolder.value = header;
		header.setSecurity(HuaweiMTOSIConnection.getSecurity());
		header.setCommunicationPattern(CommunicationPatternType.MULTIPLE_BATCH_RESPONSE);
		header.setCommunicationStyle(CommunicationStyleType.RPC);
		long batchSize = Long.parseLong(HuaweiMTOSIConnection.getTlFetchSize());
		header.setRequestedBatchSize(batchSize);
		header.setBatchSequenceNumber((long) 1);

		NamingAttributeType mdType = new NamingAttributeType();
		List<RelativeDistinguishNameType> ednList = mdType.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");
		ednList.add(mdRdn);
		request.setMlsnRef(mdType);

		TopologicalLinkRetrievalHttp service = new TopologicalLinkRetrievalHttp(
				HuaweiMTOSIConnection.getWebServiceURL(AppConstants.TL_NRF));
		TopologicalLinkRetrievalRPC port = service
				.getTopologicalLinkRetrievalRPC();
		List<TopologicalLink> tLinks = Lists.newArrayList();

		Holder<MultipleTlObjectsResponseType> responseHolder = new Holder<MultipleTlObjectsResponseType>();

		try {

			do {
				port.getAllTopologicalLinks(headerHolder, request,
						responseHolder);
				MultipleTlObjectsResponseType response = responseHolder.value;
				List<TopologicalLinkType> topologicalLinkTypes = response
						.getTlList().getTl();
				CommonLogger.info(NetworkEntiyController.class, "Discover "
						+ topologicalLinkTypes.size() + " TLs");
				CommonLogger.info(
						NetworkEntiyController.class,
						"URI interartor:"
								+ headerHolder.value.getIteratorReferenceURI());

				for (TopologicalLinkType tlType : topologicalLinkTypes) {

					TopologicalLink tlLink = new TopologicalLink();
					tlLink.setName(tlType.getName().getRdn());
					tlLink.setEmsNativeName(tlType.getAliasNameList()
							.getAlias().get(0).getAliasValue());
					tlLink.setDirection(tlType.getDirection().toString());

					if (tlType.getLayerRate() != null)
						tlLink.setLayerRate(tlType.getLayerRate().getValue());
					tlLink.setType(tlType.getType());
					tlLink.setLength(tlType.getLength());
					tlLink.setWane(tlType.getWane());
					tlLink.setAendTp(new SubTerminationPoint(tlType
							.getAEndTpRefList()));
					tlLink.setZendTp(new SubTerminationPoint(tlType
							.getZEndTpRefList()));

					// System.out.println(tlLink.toXML());
					tLinks.add(tlLink);

				}
			} while (headerHolder.value.isBatchSequenceEndOfReply()
					.booleanValue() == false);

			return tLinks;

		} catch (GetAllTopologicalLinksException e) {
			throw new U2000ProcessException("GetAllCrossConnectionsException:"
					+ e.getMessage());
		}

	}

	/**
	 * Discover all t psby ne.
	 * 
	 * @param neName
	 *            the ne name
	 * @return the list
	 * @throws U2000ProcessException
	 *             the u2000 process exception
	 * @throws MalformedURLException
	 *             the malformed url exception
	 */
	public List<TerminationPoint> discoverAllTPsbyNE(String neName)
			throws U2000ProcessException, MalformedURLException {

		Holder<Header> headerHolder = HuaweiMTOSIConnection
				.getSingleResponseHeader();

		GetAllTerminationPointsType request = new GetAllTerminationPointsType();
		NamingAttributeType neNameAttributeType = new NamingAttributeType();
		List<RelativeDistinguishNameType> rdnList = neNameAttributeType
				.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");
		rdnList.add(mdRdn);

		RelativeDistinguishNameType neRdn = new RelativeDistinguishNameType();
		neRdn.setType("ME");
		neRdn.setValue(neName);
		rdnList.add(neRdn);
		request.setMeRef(neNameAttributeType);

		if (terminationPointRetrievalHttp == null) {
			terminationPointRetrievalHttp = new TerminationPointRetrievalHttp(
					HuaweiMTOSIConnection.getWebServiceURL(AppConstants.TP_NRF));

		}

		TerminationPointRetrievalHttp service = terminationPointRetrievalHttp;
		TerminationPointRetrievalRPC port = service
				.getTerminationPointRetrievalRPC();

		Holder<MultipleTerminationPointObjectsResponseType> responseHolder = new Holder<MultipleTerminationPointObjectsResponseType>();
		responseHolder.value = new MultipleTerminationPointObjectsResponseType();

		List<TerminationPoint> terminationPoints = new ArrayList<TerminationPoint>();

		try {

			port.getAllPhysicalTerminationPointsWithoutFtps(headerHolder,
					request, responseHolder);
			MultipleTerminationPointObjectsResponseType responseType = responseHolder.value;
			List<TerminationPointType> tpList = responseType.getTpList()
					.getTp();

			CommonLogger.info(this, "Discovered TPs:" + tpList.size());

			for (TerminationPointType tp : tpList) {
				TerminationPoint terminationPoint = new TerminationPoint();
				PhysicalTerminationPointType ptpType = tp.getPtp();
				terminationPoint.setEmsNativeName(ptpType.getAliasNameList()
						.getAlias().get(0).getAliasValue());
				terminationPoint.setName(ptpType.getName().getRdn());
				terminationPoint.setDirection(ptpType.getDirection().value());
				terminationPoint.setTransParameters(ptpType
						.getTransmissionParametersList()
						.getTransmissionParameters(), true);

				terminationPoint.setIpAddress(ptpType.getIpAddressList());
				terminationPoints.add(terminationPoint);

			}

			return terminationPoints;
		} catch (GetAllPhysicalTerminationPointsWithoutFtpsException e) {
			throw new U2000ProcessException("GetAllPhysicalTerminationPointsWithoutFtpsException:"
					+ e.getMessage());
		}

	}

	/**
	 * Discover all protection groupsby ne.
	 * 
	 * @param neName
	 *            the ne name
	 * @return the list
	 * @throws U2000ProcessException
	 *             the u2000 process exception
	 * @throws MalformedURLException
	 *             the malformed url exception
	 */
	public List<ProtectionGroup> discoverAllProtectionGroupsbyNE(String neName)
			throws U2000ProcessException, MalformedURLException {
		GetAllProtectionGroupsRequest request = new GetAllProtectionGroupsRequest();
		Holder<Header> headerHolder = HuaweiMTOSIConnection
				.getSingleResponseHeader();
		NamingAttributeType mdType = new NamingAttributeType();
		List<RelativeDistinguishNameType> ednList = mdType.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");

		ednList.add(mdRdn);

		RelativeDistinguishNameType meRdn = new RelativeDistinguishNameType();
		meRdn.setType("ME");
		meRdn.setValue(neName);

		ednList.add(meRdn);

		request.setMeRef(mdType);

		try {

			if (protectionRetrieval == null) {
				protectionRetrieval = new org.tmforum.mtop.rtm.wsdl.pr.v1_0.ProtectionRetrieval_Service(
						HuaweiMTOSIConnection
								.getWebServiceURL(AppConstants.PROTECTION_NRF));
			}
			org.tmforum.mtop.rtm.wsdl.pr.v1_0.ProtectionRetrieval_Service service = protectionRetrieval;
			org.tmforum.mtop.rtm.wsdl.pr.v1_0.ProtectionRetrieval port = service
					.getProtectionRetrieval();

			Holder<GetAllProtectionGroupsResponse> responseHolder = new Holder<GetAllProtectionGroupsResponse>();

			port.getAllProtectionGroups(headerHolder, request, responseHolder);
			GetAllProtectionGroupsResponse response = responseHolder.value;
			List<ProtectionGroupType> pgTypes = response.getPgList().getPg();
			CommonLogger.info(NetworkEntiyController.class, "Discover "
					+ pgTypes.size() + " ProtctionGroups");
			List<ProtectionGroup> protectionGroups = Lists.newArrayList();

			for (ProtectionGroupType pgType : pgTypes) {
				ProtectionGroup protectionGroup = new ProtectionGroup();
				protectionGroup.setName(pgType.getName().getRdn());
				protectionGroup.setEmsNativeName(protectionGroup.getName());
				protectionGroup.setAlias(pgType.getAliasNameList().getAlias());
				protectionGroup.setOwner(pgType.getOwner());
				protectionGroup.setType(pgType.getType().getValue().toString());
				protectionGroup.setReversionMode(pgType.getReversionMode()
						.value());

				protectionGroup.setLayerRate(pgType.getLayerRate().getValue());
				protectionGroup.setParameterList(pgType.getParameterList());
				protectionGroup.setPgTunnels(pgType
						.getProtectionRelatedTpRefList());

				// System.out.println(protectionGroup.toXML());
				protectionGroups.add(protectionGroup);

			}

			return protectionGroups;

		} catch (GetAllProtectionGroupsException e) {
			throw new U2000ProcessException("ProtectionGroup:" + e.getMessage());
		}

	}

	/**
	 * Discover all traffic conditioning profile.
	 * 
	 * @return the list
	 * @throws U2000ProcessException
	 *             the u2000 process exception
	 * @throws MalformedURLException
	 *             the malformed url exception
	 */
	public List<TrafficConditioningProfile> discoverAllTrafficConditioningProfile()
			throws U2000ProcessException, MalformedURLException {
		GetAllTrafficConditioningProfileNamesRequest request = new GetAllTrafficConditioningProfileNamesRequest();
		Holder<Header> headerHolder = HuaweiMTOSIConnection
				.getSingleResponseHeader();
		NamingAttributeType mdType = new NamingAttributeType();
		List<RelativeDistinguishNameType> ednList = mdType.getRdn();
		RelativeDistinguishNameType mdRdn = new RelativeDistinguishNameType();
		mdRdn.setType("MD");
		mdRdn.setValue("Huawei/U2000");

		ednList.add(mdRdn);

		request.setMdRef(mdType);

		try {

			TrafficConditioningProfileRetrievalHttp service = new TrafficConditioningProfileRetrievalHttp(
					HuaweiMTOSIConnection
							.getWebServiceURL(AppConstants.TCP_NRF));
			TrafficConditioningProfileRetrievalRPC port = service
					.getTrafficConditioningProfileRetrievalRPC();

			Holder<GetAllTrafficConditioningProfileNamesResponse> responseHolder = new Holder<GetAllTrafficConditioningProfileNamesResponse>();

			port.getAllTrafficConditioningProfileNames(headerHolder, request,
					responseHolder);
			GetAllTrafficConditioningProfileNamesResponse response = responseHolder.value;
			List<NamingAttributeType> tcpTypes = response.getTcProfileName();
			CommonLogger.info(NetworkEntiyController.class, "Discover "
					+ tcpTypes.size() + " TrafficConditioningProfile");
			List<TrafficConditioningProfile> trafficConditioningProfiles = Lists
					.newArrayList();

			for (NamingAttributeType tcpType : tcpTypes) {
				TrafficConditioningProfile trafficConditioningProfile = new TrafficConditioningProfile(
						tcpType);
				trafficConditioningProfiles.add(trafficConditioningProfile);
			}

			return trafficConditioningProfiles;

		} catch (GetAllTrafficConditioningProfileNamesException e) {
			throw new U2000ProcessException("GetAllCrossConnectionsException:"
					+ e.getMessage());
		}

	}

}
